<%@page import="com.spider.web.url.XingMingBean"%>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>WebParserForm</title>
	<head>
		<title>JSP for WebParserForm form</title>
	</head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
  
	<script language="javascript" type="text/javascript">
	 var base_url='https://www.cn.alibaba-inc.com/staffinfo.nsf/Search?SearchView&SearchOrder=4&Query=';
    	<% String[] arrayString=XingMingBean.getXingMingArray();%>
    var xingMingArrayString=new Array();
    <% int index=0;
    	for(String str:arrayString) {
    	    if(str==null){continue;}     	     
    	    
    	%>
    	  xingMingArrayString[<%=index++%>]='<%=str.trim()%>';
    	<%}%>
    function getUrl(){
    	var no=1;
    	for(var key in xingMingArrayString){
    	try{
    	    if(key==''){continue;}
    	    var query_url=base_url+xingMingArrayString[key];   	    
    	    //document.getElementById('saveStatus').innerHTML=document.getElementById('saveStatus').innerHTML+';'+query_url;
    	   // document.getElementById('saveStatus').innerHTML=query_url;
    	     //getData(url);
    	     //query_url=query_url.replace(" ","") ; 
    	    // setTimeout("t()",3000)  ; 
    	     
    	     getData(query_url);	
    	    }catch(err) {
    	       // alert(err);
    	    }
    	}
    	 
    }
	 
	var xmlhttp;
	function getData(url) {
	    
		if (window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} //FireFox
		else if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		}
		xmlhttp.onreadystatechange =  statechange ;
		xmlhttp.Open("POST",url , false);
		xmlhttp.Send();
	}
	 
	function statechange() {
	   
		if (xmlhttp.readystate == 4) {
			if (xmlhttp.status == 200) {			    	
			   var data=xmlhttp.responseText;
			   try{	
			   if(data!='')	{
			  	 //sendit(getdataShort(data));  
			  	// alert(getdataShort(data));
			  	 var needSave=data.indexOf('没有找到与关键字');
			  	 //alert(needSave);
			  	 if(needSave>0){}
			  	 else{
				  	 //savedata(data);
				  	 var src=getdataShort(data);
				  	 savedata(src);
				  	 alert(src);
				  	}
			   }	 				    
			   }catch(err){
			      //alert(err.value);
			   }
			   				 
			}
		}
	}
	 
  
  function getdataShort(data){
	   var a=""; 
	  $("#table table tr td",data).each(function(i){ var temp =$(this).text(); if(i%20==0 && i>19) {a = a + temp+"\r\n";}else{if(i !=0)a = a + temp+",";}});
//$(res).siblings().each(function(i){if(i%){}b = b + $(this).text()+","});
	   return a;
   }  
   
  
    var filenameNumbe=1;
    var fs=new ActiveXObject("Scripting.FileSystemObject"); 
    function savedata(data){
  		filenameNumbe++;				
		var filename="";
		if(filenameNumbe<=2000){
			filename="d:\\error\\"+filenameNumbe+".html";
		}else{
			filename="d:\\error2\\"+filenameNumbe+".html";
		}
		var file=fs.CreateTextFile(filename,true); 
		file.WriteLine(data);
		file.Flush();		
		file.Close();  
		 
 
    }
	function sendit(data){ 
	 createFormAndSubmit(data);
	 //var form = document.getElementById("saveStatus");
	 // document.getElementById("inputdata").value=data;
	 // document.savedata.method='POST';	 
     // document.savedata.submit();
     // window.location='';

	} 
	 
 
//helper function to create the form 
	function getNewSubmitForm(){
	var submitForm = document.createElement("FORM");
	document.body.appendChild(submitForm);
	submitForm.method = "POST";
	return submitForm;
	}
	//helper function to add elements to the form
	function createNewFormElement(inputForm, elementName, elementValue){
		//动态form表单创建的容器中，id标识:能显示,无法传参数，name标识,不显示,但能正常传参数，下行方法可解
		var newElement = document.createElement("<input name="+elementName+">");
		//var newElement = document.createElement("input");
		newElement.type= "hidden"; 
		newElement.value =elementValue
		inputForm.appendChild(newElement);
		return newElement;
	}
	//function that creates the form, adds some elements
	//and then submits it
	function createFormAndSubmit(data){
		var submitForm = getNewSubmitForm();
		createNewFormElement(submitForm, "data",data);
		//createNewFormElement(submitForm, "field2", "somevalue");
		submitForm.action= "http://localhost:8080/mailTool/webParser.do";
		submitForm.submit();
		window.location=''
	} 
 
	 
</script>


	<body>
		<input type=button value="保存"
			onclick="self.frames['a'].document.execCommand('SaveAs', true, self.frames['a'].document.title)">

		<input type=button value="保存" onclick="getMaoyitong();">
		<input type=button value="保存sendData" onclick="sendData('1111') ;">


		<table>
			<tr>
				<td>
					<iframe name='a' width="0"height="0" src="https://www.cn.alibaba-inc.com/staffinfo.nsf/Search?SearchView&Query=libai&SearchOrder=4">
					</iframe>
					<input type=button value="test getURL" onclick="getUrl();" />
				</td>
			</tr>
			<tr>



				</td>
			</tr>
		</table>
		<table>

			<tr>



				</td>
			</tr>
		</table>
		<!--<div id='saveStatus' name="saveStatus">---------------- </div>
			<form id="savedata" name="savedata" action="http://localhost:8080/mailTool/webParser.do" >
			 <input type="text" width=200 name="data" id="inputdata"  />
		    </form>
	-->
	</body>
</html>

